package org.firebug.spring.cloud.oauth2.repository.dao;

import org.firebug.spring.cloud.oauth2.repository.po.UserPO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class UserDAO {
    private static Logger logger = LoggerFactory.getLogger("sql");
    private static RowMapper<UserPO> mapper = BeanPropertyRowMapper.newInstance(UserPO.class);
    @Resource
    private JdbcTemplate jdbcTemplate;

    public int insert(UserPO po) {
        String sql = "insert into t_user (username,password,roles) values (?,?,?)";
        Object[] args = {po.getUsername(), po.getPassword(), po.getRoles()};
        logger.info("{} {}", sql, args);
        return jdbcTemplate.update(sql, args);
    }

    public UserPO selectByUsername(String username) {
        String sql = "select * from t_user where deleted=0 and username=?";
        Object[] args = {username};
        logger.info("{} {}", sql, args);
        return jdbcTemplate.queryForObject(sql, args, mapper);
    }
}
